Every frame the game can ask each of the ten GBA buttons whether it was just pressed, is being held, or was just released, and those three answers are different things.
A GBA has no keyboard and no mouse. It has ten buttons, and GBML names them all:
| Constant | Button |
|---|---|
BTN_A, BTN_B | the two face buttons |
BTN_L, BTN_R | the two shoulder buttons |
BTN_START, BTN_SELECT | the two small middle buttons |
BTN_UP, BTN_DOWN, BTN_LEFT, BTN_RIGHT | the four directions of the d-pad |
Other consoles have other buttons. A Sega Genesis pad also has BTN_C, and on a six-button pad BTN_X, BTN_Y, BTN_Z and BTN_MODE. Use only the buttons your console has: write a name its pad does not have (BTN_C on a GBA, BTN_SELECT on a Sega Genesis) and the check stops the game, telling you which buttons that console has.
The name of a button is a constant: a fixed name that stands for a fixed value. You never change one. You hand it to a function that asks about that button, like btn_held(BTN_A).
A game does not read the buttons all the time. It reads them once per frame. A frame is one picture the GBA draws, and it draws about 60 every second, so a frame lasts a bit under 17 milliseconds. Your code runs once each frame, looks at the buttons, and decides what to do.
A person is slow compared to that. Pressing a button and letting go takes most people five or ten frames at the very least, and holding one for a second is 60 frames. So on any given frame there are three different things you may want to know about a button, and GBML has a function for each.
| Function | True when | Lasts |
|---|---|---|
btn_pressed | the button went down on this frame | one frame |
btn_held | the button is down right now | every frame it stays down |
btn_released | the button came back up on this frame | one frame |
btn_buffered(button, frames) | the button went down this frame or in the last few frames | frames frames after the press |
btn_held_frames is the odd one out: it gives a number, how many frames the button has been held, for a charge shot or a long press.
Picture a player pressing A, keeping it down for half a second, then letting go. That is about 30 frames.
btn_pressed(BTN_A) is true on the first of them and false on the other 29.btn_held(BTN_A) is true on all 30.btn_released(BTN_A) is true on the frame after the last one, once the finger is off.Suppose your player fires a bullet when A is used. If you write it with btn_held, the code runs on all 30 frames. That is 30 bullets from one press. If you write it with btn_pressed, it runs once, and the player has to press again for another bullet.
Neither is wrong. They suit different jobs.
A common beginner mistake is to use btn_held for a jump and then wonder why the character jumps again the instant it lands, with the player still holding the button down. btn_pressed fixes it.
// Step event of the player
if (btn_pressed(BTN_A)) {
vsp = -4; // jump once per press
}
if (btn_held(BTN_B)) {
hsp = 2; // run for as long as B is down
}Checking four directions one by one is tedious. btn_dx and btn_dy read the d-pad as two numbers instead.
btn_dx() is -1 with Left held, 1 with Right held, and 0 with neither.btn_dy() is -1 with Up held, 1 with Down held, and 0 with neither.Holding both Left and Right cancels to 0 rather than picking a side. The y axis is -1 for Up because on screen, y grows downward. See y. These read the buttons as held, so they suit movement:
// Step event of a walker
hsp = btn_dx() * 1.5;
vsp = btn_dy() * 1.5;The same three phases exist as events. An object can have a Button Pressed A event, a Button Held A event and a Button Released A event, one for each button and phase. An event is code that the game runs on its own when something happens. This one runs when that button reaches that phase, so it needs no if. See Button. Use an event when a whole object exists for one press, and a check inside Step when the button is one of several things that object looks at.
Naming a button in every script has a cost. If your jump is BTN_A in twelve places and the player wants it on B, you would need to change all twelve. A named action solves this. You give the action a name that says what it is for, such as jump, and you say once which button or buttons it answers to.
Actions are set up under Input actions in Project Settings: a name, plus a tick beside each button that should trigger it. In code you write the name as a plain word, with no quotes. There are three functions, and they mirror the three button questions exactly:
| Function | Same as |
|---|---|
action_pressed | btn_pressed |
action_held | btn_held |
action_released | btn_released |
// Step event of the player, using an action called jump
if (action_pressed(jump)) {
vsp = -4;
}The name is checked before the game is built, so a typo is an error and not a button that never works.
An action may have more than one button, such as jump on both B and L. Each button is watched on its own. The action counts as held while any of its buttons is down. It counts as pressed when any of them goes down, including a second button going down while the first is still held. It counts as released on the frame any one of its buttons comes up, not only once all of them have. An action with no buttons is allowed. It is never pressed, held or released, and asking about it is not an error.
Because the engine keeps its own copy of each action's buttons while the game runs, a controls screen becomes easy to write.
action_rebind(a, b) makes action a mean button b from now on. It replaces every button the action had, so afterwards only b counts.action_reset(a) puts back the buttons you chose in Project Settings.A rebinding lasts only until the game is switched off. To keep the player's choice, save it yourself with save_set when they pick, and call action_rebind again at startup. See Saving.
Every script that asks about an action, rather than a raw button, follows the rebinding automatically. A project can have up to 32 actions (more if you raise Input actions under Engine limits in Project Settings, up to 64). Details on naming rules and what is refused are in Project settings.